package com.itheima.train.manager.mq;

import com.alibaba.fastjson.JSON;
import com.itheima.train.manager.domain.OrderSynEs;
import com.itheima.train.manager.service.EsIndexService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @program: itheima-train-manager-parent
 * @description: 监听订单消费队列, 进行消费, 把数据同步到es中
 * @author: zhanghz001
 * @create: 2021-07-06 15:18
 **/
// @Service
public class ZhzEsConsumer {
    
    // 创建一个日志对象
    private Logger log = LoggerFactory.getLogger(ZhzEsConsumer.class);
    
    @Autowired
    private EsIndexService esIndexService;
    
    @RabbitListener(queues = {"train_manager_ex"})
    public void syncStock(String orderSynEsJson, Message message, Channel channel) {
        try {
            //解析json数据
            OrderSynEs orderSynEs = JSON.parseObject(orderSynEsJson, OrderSynEs.class);
            
            //调用esIndexService实现库存同步操作
            esIndexService.synStockInfo(orderSynEs);
            log.info("[同步下单扣减库存数据成功]------->{}", orderSynEsJson);
            
            //手动应答
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("[同步下单扣减库存数据失败]------->{}", orderSynEsJson);
        } finally {
            
        }
    }
    
}
